package leetcode.回溯;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wangmaolin1
 * @date 2024/3/12 22:55
 */
public class _46 {
    List<List<Integer>>result;
    public List<List<Integer>> permute(int[] nums) {
        result=new ArrayList<>();
        LinkedList<Integer>path=new LinkedList<>();
        boolean[]used=new boolean[nums.length+1];
        track(nums,path,used);
//        ReentrantLock lock2=new ReentrantLock();
//        lock2.lock();
//        ReentrantReadWriteLock lock=new ReentrantReadWriteLock();
//        lock.readLock().lock();
        return result;
    }

    private void track(int[] nums, LinkedList<Integer> path, boolean[] used) {
        if (path.size()==nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i]){
                continue;
            }
            path.add(nums[i]);
            used[i]=true;
            track(nums,path, used);
            path.removeLast();
            used[i]=false;
        }
    }
}
